package com.example.userservice.rabbitmq.publisher;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.constants.Constants;
import org.example.entity.dto.MessageDto;
import org.example.entity.dto.MessageSendDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author susu
 * @Date 2024/10/7
 */
@Component
@Slf4j
public class UserPublisher {
    @Resource
    private RabbitTemplate rabbitTemplate;


    public MessageDto userSendMsg2Group4GetInfo(MessageDto msg){
//        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_TOPIC_NAME,Constants.GROUP_QUEUE_ROUTING_KEY,msg);
        Object o = rabbitTemplate.convertSendAndReceive(Constants.EXCHANGE_CHAT_TOPIC_NAME, Constants.GROUP_QUEUE_ROUTING_KEY, msg);
        if (o instanceof MessageDto){
            return (MessageDto) o;
        }else {
            throw new IllegalArgumentException("类型转换错误，UserPublisher");
        }

    }

    public void userSendMsg2ChatDB(Object chatSession) {
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_TOPIC_NAME,Constants.GROUP_QUEUE_ROUTING_KEY,chatSession);
    }

    public void userSendMsg2Chat(Object chatMessage) {
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_TOPIC_NAME,Constants.GROUP_QUEUE_ROUTING_KEY,chatMessage);
    }

    public void sendMessage(MessageSendDto messageSendDto) {
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_TOPIC_NAME,Constants.ROUTING_KEY_CHAT,messageSendDto);
    }
}
